package com.yc.jpyy.base.util;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.jpush.api.JPushClient;
import cn.jpush.api.common.DeviceEnum;
import cn.jpush.api.push.CustomMessageParams;
import cn.jpush.api.push.MessageResult;
import cn.jpush.api.push.ReceiverTypeEnum;

/**
 * 推送类
 * 
 * @author 郭军伟
 */
public class PushNotificationTool {

	private static Logger log = LoggerFactory
			.getLogger(PushNotificationTool.class);

	// 推送key
	private static String appKey = "08d5169ba4258252063d5168";

	private static String masterSecret = "b660dbd4f1135d4271d6ba54";

	/**
	 * 推送
	 */
	public static MessageResult pushNotification(String alias, String msgTitle,
			String msgContent) {
		// String alias = "mujf";//用户账户
		// String msgTitle = "test";
		// String msgContent = "test";
		System.out.println(msgContent);
		return pushNotification(masterSecret, appKey, alias, msgTitle,
				msgContent);
	}

	/**
	 * 推送
	 */
	public static MessageResult pushNotification(String msgTitle,
			String msgContent) {
		System.out.println(msgContent);
		return pushAllUser(masterSecret, appKey, "", msgTitle, msgContent);
	}

	public static MessageResult pushAllUser(String masterSecret, String appKey,
			String alias, String msgTitle, String msgContent) {

		JPushClient jpushClient = new JPushClient(masterSecret, appKey);

		CustomMessageParams customParams = new CustomMessageParams();

		customParams.addPlatform(DeviceEnum.Android);
		customParams.setReceiverType(ReceiverTypeEnum.APP_KEY);// 根据别名推送，别名是用用户的账户
		customParams.setReceiverValue(alias);
		customParams.setTimeToLive(864000);// 超时时间
		java.util.Random r = new java.util.Random();
		customParams.setSendNo(r.nextInt(10000));

		MessageResult msgResult = jpushClient.sendCustomMessage(msgTitle,
				msgContent, customParams, null);
		log.debug("responseContent - "
				+ msgResult.responseResult.responseContent);
		if (msgResult.isResultOK()) {
			log.info("msgResult - " + msgResult);
			log.info("messageId - " + msgResult.getMessageId());
		} else {
			if (msgResult.getErrorCode() > 0) {
				// 业务异常
				log.warn("Service error - ErrorCode: "
						+ msgResult.getErrorCode() + ", ErrorMessage: "
						+ msgResult.getErrorMessage());
			} else {
				// 未到达 JPush
				log.error("Other excepitons - "
						+ msgResult.responseResult.exceptionString);
			}
		}
		return msgResult;
	}

	public static MessageResult pushNotification(String masterSecret,
			String appKey, String alias, String msgTitle, String msgContent) {

		JPushClient jpushClient = new JPushClient(masterSecret, appKey);

		CustomMessageParams customParams = new CustomMessageParams();

		customParams.addPlatform(DeviceEnum.Android);
		customParams.setReceiverType(ReceiverTypeEnum.ALIAS);// 根据别名推送，别名是用用户的账户
		customParams.setReceiverValue(alias);
		customParams.setTimeToLive(864000);// 超时时间
		java.util.Random r = new java.util.Random();
		customParams.setSendNo(r.nextInt(10000));

		MessageResult msgResult = jpushClient.sendCustomMessage(msgTitle,
				msgContent, customParams, null);
		log.debug("responseContent - "
				+ msgResult.responseResult.responseContent);
		if (msgResult.isResultOK()) {
			log.info("msgResult - " + msgResult);
			log.info("messageId - " + msgResult.getMessageId());
		} else {
			if (msgResult.getErrorCode() > 0) {
				// 业务异常
				log.warn("Service error - ErrorCode: "
						+ msgResult.getErrorCode() + ", ErrorMessage: "
						+ msgResult.getErrorMessage());
			} else {
				// 未到达 JPush
				log.error("Other excepitons - "
						+ msgResult.responseResult.exceptionString);
			}
		}
		return msgResult;
	}

	// public static MessageResult pushNotification(String masterSecret, String
	// appKey, String alias,String msgTitle, String msgContent,
	// Map<String,Object> extras){
	// JPushClient jpushClient = new JPushClient(masterSecret, appKey, 0,
	// DeviceEnum.Android, false);
	// NotificationParams params = new NotificationParams();
	// params.setReceiverType(ReceiverTypeEnum.IMEI);//根据别名推送，别名是用用户的账户
	// params.setAndroidNotificationTitle(msgTitle);
	// params.setAndroidBuilderId(66);
	// params.setTimeToLive(864000);//超时时间
	// params.setReceiverValue(alias);
	// java.util.Random r=new java.util.Random();
	// params.setSendNo(r.nextInt(10000));
	// // params.setTimeToLive(0);
	//
	// MessageResult msgResult = jpushClient.sendNotification(msgContent,
	// params, extras);
	// log.debug("responseContent - " +
	// msgResult.responseResult.responseContent);
	// if (msgResult.isResultOK()) {
	// log.info("msgResult - " + msgResult);
	// log.info("messageId - " + msgResult.getMessageId());
	// } else {
	// if (msgResult.getErrorCode() > 0) {
	// // 业务异常
	// log.warn("Service error - ErrorCode: "
	// + msgResult.getErrorCode() + ", ErrorMessage: "
	// + msgResult.getErrorMessage());
	// } else {
	// // 未到达 JPush
	// log.error("Other excepitons - " +
	// msgResult.responseResult.exceptionString);
	// }
	// }
	// return msgResult;
	// }

	public static MessageResult pushCustomMessage(String masterSecret,
			String appKey, String alias, String msgTitle, String msgContent,
			Map<String, Object> extras) {
		JPushClient jpushClient = new JPushClient(masterSecret, appKey, 0,
				DeviceEnum.Android, false);
		CustomMessageParams params = new CustomMessageParams();
		params.setReceiverType(ReceiverTypeEnum.ALIAS);// 根据别名推送，别名是用用户的账户
		params.setTimeToLive(864000);// 超时时间
		params.setReceiverValue(alias);
		java.util.Random r = new java.util.Random();
		params.setSendNo(r.nextInt(10000));

		MessageResult msgResult = jpushClient.sendCustomMessage(msgTitle,
				msgContent, params, extras);
		log.debug("responseContent - "
				+ msgResult.responseResult.responseContent);
		if (msgResult.isResultOK()) {
			log.info("msgResult - " + msgResult);
			log.info("messageId - " + msgResult.getMessageId());
		} else {
			if (msgResult.getErrorCode() > 0) {
				// 业务异常
				log.warn("Service error - ErrorCode: "
						+ msgResult.getErrorCode() + ", ErrorMessage: "
						+ msgResult.getErrorMessage());
			} else {
				// 未到达 JPush
				log.error("Other excepitons - "
						+ msgResult.responseResult.exceptionString);
			}
		}
		return msgResult;
	}

	public static void main(String[] args) throws Exception {
		pushNotification("28","","XTGG_KCBH-14386813092_PC端取消预约");
		System.out.println("------------------推送成功------------------");
	}
}
